package com.kai.steering.common.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @ Author     ：LiuKai.
 * @ Date       ：Created in 14:38 2022-3-10
 * @ Description：
 * @ Modified By：
 * @Version:
 */
@Slf4j
public class JsonUtils {

    private JsonUtils() {
    }

    // 定义jackson对象
    public static final ObjectMapper MAPPER = new ObjectMapper();


    public static JsonNode str2JsonNode(String data) {
        JsonNode result = null;

        try {
            result = MAPPER.readTree(data);
        } catch (JsonProcessingException e) {
            log.error("JsonNode解析异常", e);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }


    public static JsonNode obj2JsonNode(Object data) {
        return str2JsonNode(toJsonString(data));
    }


    /**
     * 将json结果集转化为对象
     *
     * @param jsonData json数据
     * @param beanType 对象中的object类型
     * @return
     */
    public static <T> T jsonToPojo(String jsonData, Class<T> beanType) {
        try {
            return MAPPER.readValue(jsonData, beanType);
        } catch (Exception e) {
            log.error("jsonToPojo异常", e);
        }
        return null;
    }

    /**
     * 将json数据转换成pojo对象list
     * <p>Title: jsonToList</p>
     * <p>Description: </p>
     *
     * @param jsonData
     * @param beanType
     * @return
     */
    public static <T> List<T> jsonToList(String jsonData, Class<T> beanType) {
        JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
        try {
            return MAPPER.readValue(jsonData, javaType);
        } catch (Exception e) {
            log.error("jsonToList异常", e);
        }
        return new ArrayList<>();
    }


    /**
     * 键按自然顺序输出
     *
     * @param object
     * @return
     */
    public static String toJsonString(Object object) {
        return toJsonString(object, false, null);
    }


    public static String toJsonString(Object object, String dateFormat) {
        return toJsonString(object, false, dateFormat);
    }


    /**
     * 键按自然顺序格式化输出
     *
     * @param object
     * @param prettyFormat
     * @return
     */
    public static String toJsonString(Object object, boolean prettyFormat, String dateFormat) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (StringUtils.isNotEmpty(dateFormat)) {
                objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
            }
            //格式化输出
            objectMapper.configure(SerializationFeature.INDENT_OUTPUT, prettyFormat);
            //键按自然顺序输出
            objectMapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, false);
            return objectMapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            log.error("", e);
        }
        return null;
    }


}